<template>
    <div>
        {{ avsd }}
        <table class="table table-bordered ">
            <thead>
                <tr>
                    <td>会员编号</td>
                    <td><input type="text" v-model="avsd.number" disabled ></td>
                </tr>

                <tr>
                    <td>会员姓名</td>
                    <td><input type="text" v-model="avsd.dName"></td>
                </tr>

                <tr>
                    <td>会员号码</td>
                    <td><input type="text" v-model="avsd.phone"></td>
                </tr>

                <tr>
                    <td>身份号</td>
                    <td><input type="text" v-model="avsd.shfn"></td>
                </tr>

                <tr>
                    <td>会员生日</td>
                    <td><input type="date" v-model="avsd.data"></td>
                </tr>

                <tr>
                    <td>会员性别</td>
                    <td><input type="radio" v-model="avsd.dSex" :value="1">男
                        <input type="radio" v-model="avsd.dSex" :value="0">女
                    </td>
                </tr>

                <tr>
                    <td>会员爱好</td>
                    <td><input type="checkbox" v-model="avsd.ai" name="ai" value="吃">吃
                        <input type="checkbox" v-model="avsd.ai" name="ai" value="喝">喝
                        <input type="checkbox" v-model="avsd.ai" name="ai" value="睡">睡
                        <input type="checkbox" v-model="avsd.ai" name="ai" value="梦想成真">梦想成真
                    </td>
                </tr>

                <tr>
                    <td align="right">会员等级</td>
                    <td><select v-model="avsd.dId">
                            <option value="0">请选择</option>
                            <option :value="o.dId" v-for="o in find">{{ o.name }}</option>
                        </select></td>
                </tr>

                <tr>
                    <td>会员积分</td>
                    <td><input type="text" v-model="avsd.fen"></td>
                </tr>

        <tr>
                    <td>会员余额</td>
                    <td><input type="text" v-model="avsd.money"></td>
                </tr>

                <tr>
                    <td>会员简介</td>
                    <td><textarea v-model="avsd.text"></textarea></td>
                </tr>
                <tr>
                    <td>同意条款</td>
                    <td><input type="checkbox" v-model="avsd.tiao"></td>
                </tr>

                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="Add"></td>
                </tr>
            </thead>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';

const route= useRouter()

let avsd: any = ref({
    "number": moment(new Date()).format("yyyyMMDD")+Math.random().toString().substring(2,5),
    "dName": "",
    "phone": "",
    "shfn": "",
    "data": "",
    "dSex": "",
    "ai": [],
    "dId": "0",
    "fen": "",
    "money": "",
    "text": "",
    "tiao": "",
    "state": 1,
    "isDell": 0
});

onMounted(() => {
    List();
})


let find: any = ref([]);

const Add = () => {

    

    if (avsd.value.dName == "") {
        alert("姓名不为空");
        return;
    }
    let reg = /^[\u4e00-\u9fa5]{2,20}$/;
    if(reg.test(avsd.value.dName)==false)
    {
        alert("姓名只能是2-20个汉字");
        return;
    }
    if (avsd.value.phone == "") {
        alert("手机号不为空");
        return;
    }
    let reg1 = /^1[3-9]\d{9}$/;
    if(reg1.test(avsd.value.phone)==false )
    {
        alert("手机号格式不为空");
        return;
    }
    if (avsd.value.shfn == "") {
        alert("身份证不为空");
        return;
    }
    let reg2=/^\d{17}[0-9xX]$/;
    if(reg2.test(avsd.value.shfn)==false )
    {
        alert("身份证格式不正确");
        return;
    }

    if (avsd.value.data == "") {
        alert("时间不为空");
        return;
    }
    if (avsd.value.dSex == "") {
        alert("性别不为空");
        return;
    }
    if (avsd.value.ai.length==0) {
        alert("爱好不为空");
        return;
    }
    
    if (avsd.value.fen == "") {
        alert("积分不为空");
        return;
    }
    let reg3 = /^[1-9]\d*$/;
    if(reg3.test(avsd.value.fen)==false )
    {
        alert("积分必须是正数");
        return;
    }
    if (avsd.value.money == "") {
        alert("余额不为空");
        return;
    }
    let reg4 =/^[1-9]\d*(\.\d+)?|0\.\d+[1-9]\d*$/;
    if(reg4.test(avsd.value.money)==false )
    {
        alert("余额必须是正数");
        return;
    }
    if (avsd.value.text == "") {
        alert("简介不为空");
        return;
    }
    if (avsd.value.text.length>50 ) {
        alert("会员简介不能大于50字符");
        return;
    }
    if (avsd.value.tiao == false) {
        alert("必须同意条款");
        return;
    }
    avsd.value.ai=avsd.value.ai.toString();
    axios({
        url: 'https://localhost:7033/api/User/AddDaed',
        method: 'post',
        data: avsd.value
    })
        .then((res) => {
            
            console.log(res);
            if (res.data > 0) {
                alert("成功");
                route.push("/DaedShow");
                return;
            }
            if (res.data == -1) {
                alert("会员名称相同");
                return;
            }
            
        })
}






const List = () => {

    axios({
        url: 'https://localhost:7033/api/User/Luist',
        method: 'get',
    })

        .then((res) => {
            console.log(res);
            find.value = res.data
        })
}


</script>

<style scoped></style>